package leetcode.leetcode_429;



import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        LinkedList<Node> nodes = new LinkedList<>();
        nodes.add(root);
        while(!nodes.isEmpty()){
            int size = nodes.size();
            ArrayList<Integer> integers = new ArrayList<>();
            for (int i =0;i<size;i++){
                Node poll = nodes.poll();
                integers.add(poll.val);
                for (Node child : poll.children) {
                    if (child != null) {
                        nodes.add(child);
                    }
                }
            }
            list.add(integers);
        }
        return list;

    }
}